#include "ProgBar.h"
#include "ResourceManagers.h"

ProgBar::ProgBar(sf::Image *bar, sf::Image *frame, bool horizontal)
{
//    *ImageManager::Instance()->GetImage("temp_bar.png")
	m_bar= new Sprite(*bar);
	m_frame= new Sprite(*frame);
	m_bar->SetScale(0.5f,0.5f);
	m_frame->SetScale(0.5f, 0.5f);
	m_bar->SetColor(Color(255,255,255,200));
	//m_frame->SetColor(Color(255,255,255,157));

	SetPosition(Vector2f(0,0));
    this->horizontal=horizontal;
}


ProgBar::~ProgBar(void){
    delete m_bar;
    delete m_frame;
}

void ProgBar::SetMaxMin(float _max, float _min){
	max=_max;
	min=_min;
}

void ProgBar::SetValue(float val){
	IntRect sub;
	if(horizontal){
        sub.Left=0;
        sub.Top=0;
        sub.Bottom=m_bar->GetImage()->GetHeight();
        float scale=(val-min)/(max-min);
        if( scale<0)
            sub.Right=0;
        if(scale>1)
            sub.Right=1;

        sub.Right= scale* m_bar->GetImage()->GetWidth();
	}else{
	    // NO ANDA UN CARAJO
//	    m_bar->SetCenter(0,m_bar->GetImage()->GetHeight());
//        sub.Left=0;
//        sub.Bottom=m_bar->GetImage()->GetHeight();
//        float scale=(val-min)/(max-min);
//        if( scale<0)
//            sub.Right=0;
//        if(scale>1)
//            sub.Right=1;
//        sub.Top=sub.Bottom+m_bar->GetImage()->GetHeight()*scale;
//        sub.Right=m_bar->GetImage()->GetWidth();
//        m_bar->SetPosition(200,200);

	}

	m_bar->SetSubRect(sub);
}

void ProgBar::Draw(RenderWindow *wnd){
	wnd->Draw(*m_bar);
	wnd->Draw(*m_frame);
}

void ProgBar::Update(float dt){

}

void ProgBar::SetPosition(Vector2f pos){
	m_frame->SetPosition(pos);
	m_bar->SetPosition(pos);
}
